package com.example.deepai.service;

import com.example.deepai.model.UIElement;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.example.deepai.util.ImageUtils;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;  
import java.util.HashMap;   

@Slf4j
@Service
@RequiredArgsConstructor
public class EnhancedUIRecognitionService {
    private final UIRecognitionService basicRecognitionService;
    private final OpenCVUIDetectionService opencvService;
    private final VectorDBService vectorDBService;
    private final QwenVisionService qwenService;
    
    public Map<String, Object> analyzeUI(String screenshotPath) {
        try {
            // 1. OpenCV基础检测
            List<UIElement> opencvElements = 
                opencvService.detectUIElements(screenshotPath);
            
            // 2. 通义千问分析
            Map<String, Object> aiAnalysis = 
                basicRecognitionService.recognizeUI(screenshotPath);
            
            // 增加通义千问的语义理解
            String semanticAnalysis = qwenService.analyzeImage(
                ImageUtils.toBase64(screenshotPath),
                "请分析这些UI组件的功能和用途"
            );
            aiAnalysis.put("semantic_analysis", semanticAnalysis);
            
            // 3. 向量数据库查询相似组件
            List<UIElement> similarElements = 
                findSimilarComponents(opencvElements);
            
            // 4. 合并结果
            return mergeResults(opencvElements, aiAnalysis, similarElements);
            
        } catch (Exception e) {
            log.error("增强UI分析失败", e);
            return Map.of("error", "分析失败: " + e.getMessage());
        }
    }
    
    // 添加特征向量提取方法
    private float[] extractFeatureVector(UIElement element) {
        // 创建特征向量
        float[] vector = new float[512]; // 与Milvus配置相匹配的维度
        
        // 添加位置特征
        vector[0] = element.getX() / 1920f; // 归一化x坐标
        vector[1] = element.getY() / 1080f; // 归一化y坐标
        vector[2] = element.getWidth() / 1920f; // 归一化宽度
        vector[3] = element.getHeight() / 1080f; // 归一化高度
        
        // 添加类型特征
        switch (element.getType()) {
            case "button":
                vector[4] = 1.0f;
                break;
            case "text_field":
                vector[5] = 1.0f;
                break;
            // 添加其他类型的特征
        }
        
        return vector;
    }
    
    private List<UIElement> findSimilarComponents(List<UIElement> elements) {
        List<UIElement> similarElements = new ArrayList<>();
        for (UIElement element : elements) {
            float[] featureVector = extractFeatureVector(element);
            similarElements.addAll(
                vectorDBService.findSimilarComponents(featureVector));
        }
        return similarElements;
    }
    
    private Map<String, Object> mergeResults(
            List<UIElement> opencvElements,
            Map<String, Object> aiAnalysis,
            List<UIElement> similarElements) {
        Map<String, Object> result = new HashMap<>();
        result.put("opencv_elements", opencvElements);
        result.put("ai_analysis", aiAnalysis);
        result.put("similar_elements", similarElements);
        return result;
    }
}